package com.lm.scau.framedemos.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lm.scau.framedemos.BaseActivity;
import com.lm.scau.framedemos.R;
import com.lm.scau.framedemos.Views.ViewHolder;

import java.util.List;

/**
 * Function:
 * auth：liming on 2016/5/30 13:55
 * mail：liming@wxchina.com
 */
public class LMGridViewAdapter extends BaseAdapter {

    private static final Integer IMG = R.mipmap.ic_app_movie;
    private LayoutInflater layoutInflater;
    private BaseActivity mActivity;
    private List<String> titles;
    private List<Integer> imgs;

    public LMGridViewAdapter(BaseActivity avtivity, List<String> titles, List<Integer> imgs) {
        this.mActivity = avtivity;
        if (null != imgs) {
            this.imgs = imgs;

        }
        this.titles = titles;
        this.layoutInflater = LayoutInflater.from(avtivity);
    }

    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public Object getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.item_grid_view, parent, false);
        }
        TextView name = ViewHolder.getHolderView(convertView, R.id.tv_item);
        ImageView img = ViewHolder.getHolderView(convertView, R.id.iv_item);
        initData(name, img, position);
        return convertView;
    }

    private void initData(TextView name, ImageView img, int position) {
        name.setText(titles.get(position));
        if (null == imgs) {
            img.setBackgroundResource(IMG);
        } else {
            img.setBackgroundResource(imgs.get(position));
        }
    }
}
